package com.dhcc.bpm.modules.mail.service.impl;

import com.dhcc.bpm.modules.mail.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @ClassName MailServiceImpl
 * @Description 邮件推送业务层实现类
 * @Author lijiacheng
 * @Date 2020/2/25 15:22
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@Slf4j
@Service
public class MailServiceImpl implements MailService {

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender mailSender; // 用于发送文件

    /**
     * @Description 发送普通文本邮件
     * @Author LiJiaCheng
     * @Date 2020/2/26 11:35
     * @param to 收件人
     * @param subject 主题
     * @param content 内容
     * @return void
     **/
    @Override
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        //收信人
        message.setTo(to);
        //主题
        message.setSubject(subject);
        //内容
        message.setText(content);
        //发信人
        message.setFrom(from);
        mailSender.send(message);
    }

    /**
     * @Description 发送HTML邮件
     * @Author LiJiaCheng
     * @Date 2020/2/26 11:40
     * @param to 收件人
     * @param subject 主题
     * @param content 内容
     * @return void
     **/
    @Override
    public void sendHtmlMail(String to, String subject, String content,String emailUrl) {
        log.info("发送HTML邮件开始：{},{},{}", to, subject, content,emailUrl);
        //使用MimeMessage，MIME协议
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper;
        //MimeMessageHelper帮助我们设置更丰富的内容
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            //true代表支持html
            helper.setText(content, true);
            helper.setText(emailUrl, true);
            mailSender.send(message);
            log.info("发送HTML邮件成功");
        } catch (MessagingException e) {
            log.error("发送HTML邮件失败：", e);
        } finally {

        }
    }
    @Override
    public void sendHtmlMail(String to, String subject, String content) {
        log.info("发送HTML邮件开始：{},{},{}", to, subject, content);
        //使用MimeMessage，MIME协议
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper;
        //MimeMessageHelper帮助我们设置更丰富的内容
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            //true代表支持html
            helper.setText(content, true);
            mailSender.send(message);
            log.info("发送HTML邮件成功");
        } catch (MessagingException e) {
            log.error("发送HTML邮件失败：", e);
        } finally {

        }
    }

    /**
     * @Description 发送带附件的邮件
     * @Author LiJiaCheng
     * @Date 2020/2/26 11:42
     * @param to  收件人
     * @param subject 主题
     * @param content 内容
     * @param filePath 附件路径
     * @return void
     **/
    @Override
    public void sendAttachmentMail(String to, String subject, String content, String filePath) {

    }

    /**
     * @Description 发送带图片的邮件
     * @Author LiJiaCheng
     * @Date 2020/2/26 11:43
     * @param to 收件人
     * @param subject 主题
     * @param content 文本
     * @param rscPath 图片路径
     * @param rscId 图片ID，用于在<img>标签中使用，从而显示图片
     * @return void
     **/
    @Override
    public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {

    }
}
